//If the SDK namespace object is not defined, create it.
if (typeof (MCS) == "undefined")
{ MCS = {}; }
// Create Namespace container for functions in this library;
MCS.Facility = {};

MCS.Facility.FilterLookups = function () {
    var COSViewId = Xrm.Page.getAttribute("mcs_businessunitid");
    var FTCViewId = Xrm.Page.data.entity.getId();
    var CPViewId = Xrm.Page.getAttribute("createdby");

    if (COSViewId == null || FTCViewId == null || CPViewId == null)
        return;
    COSViewId = COSViewId.getValue()[0].id;
    //FTCViewId = FTCViewId.getValue()[0].id;
    CPViewId = CPViewId.getValue()[0].id;

    var facility = Xrm.Page.getAttribute("mcs_name").getValue();
    var fetchBase = null;
    //Create Fetch and set the lookup view
    var columns = [
        'teamid',
        'name',
        'createdon'
    ];
    var conditions = [
        '<condition attribute="cvt_facility" value="' + Xrm.Page.data.entity.getId() + '" uitype="mcs_facility" uiname="' + facility + '" operator="eq"/>',
    ];


    var TeamlayoutXml = '<grid name="resultset" object="9" jump="name" select="1" icon="0" preview="0"><row name="result" id="teamid"><cell name="name" width="300"/><cell name="createdon" width="100"/></row></grid>';

    conditions[1] = '<condition attribute="cvt_type" operator="eq" value="917290000" />';
    fetchBase = MCS.cvt_Common.CreateFetch('team', columns, conditions, ['name', false]);
    var FTCControl = Xrm.Page.ui.controls.get("cvt_ftcteam");
    FTCControl.addCustomView(FTCViewId, "team", "FTC at " + facility, fetchBase, TeamlayoutXml, true);

    conditions[1] = '<condition attribute="cvt_type" operator="eq" value="917290002" />';
    fetchBase = MCS.cvt_Common.CreateFetch('team', columns, conditions, ['name', false]);
    var chiefofStaffControl = Xrm.Page.ui.controls.get("cvt_chiefofstaffteam");
    chiefofStaffControl.addCustomView(COSViewId, "team", "Chief of Staff at " + facility, fetchBase, TeamlayoutXml, true);

    conditions[1] = '<condition attribute="cvt_type" operator="eq" value="917290003" />';
    fetchBase = MCS.cvt_Common.CreateFetch('team', columns, conditions, ['name', false]);
    var CPControl = Xrm.Page.ui.controls.get("cvt_cpteam");
    CPControl.addCustomView(CPViewId, "team", "C&P Officers at " + facility, fetchBase, TeamlayoutXml, true);

}